package org.microdream.easy.deploy.service;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import ice.server.comm.BaseServiceImpl;
import ice.server.comm.CommRepository;
import ice.server.comm.ParamBulider;
import ice.tool.ResultMap;
import org.apache.commons.lang3.StringUtils;
import org.microdream.easy.deploy.dao.DFunctionRepository;
import org.microdream.easy.deploy.po.DFunction;
import org.microdream.easy.deploy.utils.DateUtil;
import org.microdream.easy.deploy.utils.ListUtil;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@Service
public class DFunctionService extends BaseServiceImpl<DFunction, Integer>{

    @Resource
    private DFunctionRepository dFunctionRepository;

    @Override
    public CommRepository<DFunction, Integer> getRepository() {

        return dFunctionRepository;
    }

    @Override
    public Integer getIdenty(DFunction t) {
        return t.getId();
    }

    public Map add(DFunction dFunction){
        Integer flag = this.save(dFunction);
        return ResultMap.build().add(flag>0).get();
    }

    public Map upd(DFunction dFunction){
        Integer flag = this.save(dFunction);
        return ResultMap.build().update(flag>0).get();
    }

    public Map del(List<String> ids){
        List<DFunction> dFunctions = this.find(ParamBulider.build().add("functionCode_in", ListUtil.ListToString(ids,",")).get());
        if(CollectionUtils.isEmpty(dFunctions)){
            return ResultMap.build().warningState().message("无效数据").get();
        }
        dFunctions.forEach(dFunction -> {
            dFunction.setIsLock(1);
        });
        return ResultMap.build().delete(this.save(dFunctions)).get();
    }

    public JSONArray getOrgKeyValue(String rentCode, String keyName, String valueName){
        JSONArray jsonArray = new JSONArray();
        List<DFunction> dOrgs = this.find(ParamBulider.build().addEqual("rentCode",rentCode).addEqual("isLock",0).get());
        jsonArray = build("0",dOrgs,"nodes",keyName,valueName);
        return jsonArray;
    }

    private JSONArray build(String fcode,List<DFunction> dFunctions, String childrenKey,String keyName, String valueName){
        JSONArray jsonArray = new JSONArray();
        dFunctions.stream().forEach(dFunction -> {
            if(dFunction.getFunctionFCode().equals(fcode)){
                JSONObject jsonObject = new JSONObject();
                jsonObject.put(StringUtils.isNotEmpty(keyName)?keyName:"myId",dFunction.getFunctionCode());
                jsonObject.put(StringUtils.isNotEmpty(valueName)?valueName:"text",dFunction.getFunctionName());
                JSONArray ja = build(dFunction.getFunctionCode(),dFunctions, childrenKey,keyName,valueName);
                if(!ja.isEmpty()) {
                    jsonObject.put(childrenKey, ja);
                }
                jsonArray.add(jsonObject);
            }
        });
        return jsonArray;
    }

    public DFunction searchByCode(String key){
        DFunction dFunction = this.findSingle(ParamBulider.build().addEqual("functionCode",key).get());
        if(dFunction.getUpdateTime()!=null){
            dFunction.set("showUpdateTime", DateUtil.TimestampToString_1(dFunction.getUpdateTime()));
        }
        if(dFunction.getFunctionFCode()!=null && !dFunction.getFunctionFCode().equals("0")){
            DFunction dFunctionF = this.findSingle(ParamBulider.build().addEqual("functionCode",dFunction.getFunctionFCode()).get());
            dFunction.set("showFunctionFCode", dFunctionF.getFunctionName()!=null?dFunctionF.getFunctionName():"无");
        }else {
            dFunction.set("showFunctionFCode","无");
        }
        return dFunction;
    }
}
